<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/2/8
 * Time: 0:29
 */

namespace app\api\controller\driver;
use app\api\logic\Driver;
use app\api\model;

/**
 * @name【司机订单接口】
 * @baseurl【driver.index】
 * @file【driver/Order.php】
 */
class Order extends Base
{
    /**
     * @name【订单费用】
     * @desc【订单费用】
     * @uri【getOrderFee】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1 #
     * orderid $ int $ 5 $订单id$ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息 #
     * data $ json $ $ 费用明细 $ {"order_fee_id":79096,"order_id":23737,"pre_meter":"6835.0","pre_time":"771.0","pre_fee":"53.08","order_fee":"0.00","wait_time":0,"wait_fee":"0.00","wait_fee2":"0.00","start_fee":"9.00","start_fee2":"5.00","base_time":0,"base_meter":"0.00","use_time":0,"time_fee":"0.00","time_fee2":"0.00","meter":"0.00","travel_fee":"9.00","remote_meter":"0.00","remote_fee":"0.00","remote_fee2":"0.00","travel_fee2":"5.00","low_speed_time":0,"low_fee":"0.00","low_fee2":"0.00","night_meter":"0.00","night_fee":"0.00","night_fee2":"0.00","high_fee":"0.00","toll":"0.00","park_fee":"0.00","other_fee":"0.00","other_note":"","cancel_time":0,"cancel_fee":"0.00","driver_money":"5.00","passenger_money":"9.00","company_money":"4.00","order_money":"0.00","discount":0,"update_time":"1970-01-01 08:33:38"}
     * 】
     */
    public function getOrderFee(){
        $oid         = input('orderid');
        $orderFee    = (new model\OrderFee())->getDriverFee($oid);
        $data['data']= $orderFee;
        return $this->retSucc($data);
    }

    /**
     * @name【按订单获取轨迹】
     * @desc【按订单获取轨迹】
     * @uri【getTrackByOrder】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1 #
     * orderid $ int $ 5 $订单id$ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息 #
     * night $ json $ $ 夜间里程 #
     * low $ json $ $ 低速时间#
     * data $ json $ $ 轨迹 $[{"driver_id":"17","longitude":"118.098569","latitude":"24.467859","speed":"0","time":"1517983167581","accuracy":"15","altitude":"0","bearing":"0","city":"厦门市","provider":"lbs","mile":"0"},{"driver_id":"17","longitude":"118.09969835069","latitude":"24.466397840712","speed":"0.17000000178814","time":"1517983170186","accuracy":"37.925","altitude":"289","bearing":"54.84","city":"厦门市","provider":"gps","mile":"0"}]
     * 】
     */
    public function getTrackByOrder(){
        $oid          = input('orderid',0);
        $order        = model\Order::get($oid);
        $gps          = model\OrderGps::get(['order_id'=>$oid]);
        list($data['mile'],$data['low'],$data['night'],$data['data']) = (new \app\api\logic\Gps())->getTrack($order->driver_id,$gps->go_time,$gps->arrival_time);
        return $this->retSucc($data);
    }

    /**
     * @name【订单详情】
     * @desc【订单详情】
     * @uri【orderDetail】
     * @method【post】
     * @param【
     * dtoken  $ string $ abcd $ 校验码 $ 1 #
     * orderid $ int    $ 10   $ 订单id $ 1
     * 】
     * @return【
     * code $ int    $ number|1-1 $ 校验码#
     * msg  $ string $  $ 错误信息#
     * data $ string $  $ 详情 ${"driver_id":17,"passenger_name":"小黄","passenger_phone":"15060816655","from_lon":"118.17962300","from_lat":"24.50950800","from_addr":"高林社区","to_lon":"118.12885600","to_lat":"24.50588300","to_addr":"乌石浦油画村","use_time":"2018-02-06 21:20:00","go_time":"2018-02-07 13:55:27","driver_money":"10.00","order_state":8}
     * 】
     */
    public function orderDetail() {
        $order_id = input('orderid');
        $order    = (new model\Order())->getOrderDetail($order_id);
        if ($order['driver_id'] != $this->driverInfo['driver_id']) {
            return $this->retError(405);
        }
        $det['data'] = $order;
        return $this->retSucc($det);
    }

    /**
     * @name【订单列表】
     * @desc【订单列表】
     * @uri【orderList】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1 #
     * car_type_id $ int $ 0 $ 车辆类型 #
     * start_time $ datetime $ 2018-01-01 12:12:12 $ 开始时间#
     * end_time $ datetime $ 2018-01-01 16:12:12 $ 结束时间#
     * order_state $ int $ 5 $ 订单状态[传多值5,6,7] #
     * typeid $ int $ 1 $ 用车类型[传多值5,6,7] #
     * use_type $ int $ 1 $ 用车类型[传多值5,6,7] #
     * booking_type $ int $ 1 $ 预定类型[传多值5,6,7] #
     * page $ int $ 1 $ 页数 #
     * pagenum $ int $ 10 $ 记录数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息#
     * data $ string $ $ 列表$[{"order_id":1,"order_state":1,"order_type":"0","use_type":"1","booking_type":"0","order_time":null,"car_type_id":28,"departure":"晋江内坑","destination":"石狮"},
     * {"order_id":2,"order_state":5,"order_type":"0","use_type":"0","booking_type":"0","order_time":"2017-12-31 08:17:56","car_type_id":null,"departure":"泉州","destination":"晋江"}]
     * 】
     */
    public function orderList() {
        $paramStr = 'car_type_id,start_time,end_time,order_state,typeid,use_type,booking_type,page|1,pagenum|10';
        $param    = $this->getMassParam($paramStr);
        list($car_type_id, $start_time, $end_time, $order_state, $use_type, $order_type, $booking_type, $page, $pageSize) = $param;
        if (empty($this->dtoken)) {
            return $this->retError(501);
        }
        list($data['counts'], $data['data']) = (new model\Order())->getOrderList($this->driverInfo['driver_id'], $car_type_id, $start_time, $end_time, $order_state, $use_type, $order_type, $booking_type, $page, $pageSize);
        return $this->retSucc($data);
    }

    /**
     * @name【剩余里程和时间】
     * @desc【剩余里程和时间】
     * @uri【getLeftDistance】
     * @method【post】
     * @param【
     * dtoken $ string $ abcd $ 校验码 $ 1 #
     * from_str $ string $ 129.235677,36345678 $ 出发地坐标[小数不超6位] $ 1#
     * to_str $ string $ 129.235677,36345678 $ 目的地坐标 $ 1
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息
     * 】
     */
    public function getLeftDistance(){
        $a1  = input('from_str','');
        $a2  = input('to_str','');
        if(empty($a1) || empty($a2)) return $this->retError(136);
        $url = 'http://restapi.amap.com/v3/direction/driving?key=07e365ae157529cdb4c53be4a31f75c5&origin=';
        $url.= $a1.'&destination='.$a2.'&originid=&destinationid=&extensions=base&strategy=10';
        $data = file_get_contents($url);
        $o    = json_decode($data);
        $da['meter'] = $o->route->paths[0]->distance;
        $da['time']  = $o->route->paths[0]->duration;
        return $this->retSucc(['data'=>$da]);
    }
}